package com.dqp.cslggroup.Util;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;

/**
 * @author 董琴平
 * @version 1.0
 * @date 2021/9/26 16:56
 */
public class IPUtil {

    public static boolean isHostReachable(String host, Integer timeOut) {
        try {
            return InetAddress.getByName(host).isReachable(timeOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getIpByNmae(String name) {
        InetAddress address = null;
        try {
            address = InetAddress.getByName(name);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
        return address.getHostAddress();
    }

    public static boolean isHostConnectable(String host, int port) {
        Socket socket = new Socket();
        try {
            InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port);
            socket.connect(inetSocketAddress, 1000);
        } catch (IOException e) {
//            e.printStackTrace();
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
//                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

}
